﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// UI窗口事件监听器：用于监听UI窗口的状态事件
    /// 如果要监听指定的UI窗口开启，关闭状态，可以在此注册回调事件
    /// </summary>
    public class UISystemEvent 
    {
        public static Dictionary<UIEvent, UICallBack> s_allUIEvents = new Dictionary<UIEvent, UICallBack>();
        public static Dictionary<string, Dictionary<UIEvent, UICallBack>> s_singleUIEvents = new Dictionary<string, Dictionary<UIEvent, UICallBack>>();

        /// <summary>
        /// 注册所有事件，每个UI窗口状态都会派发
        /// </summary>
        /// <param name="uiEvent">指定监听的UI窗口状态</param>
        /// <param name="callback">回调</param>
        public static void RegisterAllUIEvent(UIEvent uiEvent,UICallBack callback)
        {
            if (s_allUIEvents.ContainsKey(uiEvent))
            {
                s_allUIEvents[uiEvent] += callback;
            }
            else
            {
                s_allUIEvents.Add(uiEvent, callback);
            }
        }

        public void RemoveAllUIEvent(UIEvent uiEvent,UICallBack callback)
        {
            if (s_allUIEvents.ContainsKey(uiEvent))
            {
                s_allUIEvents[uiEvent] -= callback;
            }
            else
            {
                Debug.Log("Error: RemoveAllEvent don't exits: " + uiEvent);
            }
        }
        /// <summary>
        /// 注册单个UI窗口事件
        /// </summary>
        /// <param name="uiName">指定所要监听的UI窗口名，一般是UI窗口对象名</param>
        public static void RegisterSingleUIEvent(string uiName,UIEvent uiEvent, UICallBack callback)
        {
            if (s_singleUIEvents.ContainsKey(uiName))
            {
                if (s_singleUIEvents[uiName].ContainsKey(uiEvent))
                {
                    s_singleUIEvents[uiName][uiEvent] += callback;
                }
                else
                {
                    s_singleUIEvents[uiName].Add(uiEvent, callback);
                }
            }
            else
            {
                s_singleUIEvents[uiName] = new Dictionary<UIEvent, UICallBack>();
                s_singleUIEvents[uiName].Add(uiEvent, callback);
            }
        }

        
        public static void RemoveSingleUIEvent(string uiName,UIEvent uiEvent,UICallBack callback)
        {
            if (s_singleUIEvents.ContainsKey(uiName))
            {
                if (s_singleUIEvents[uiName].ContainsKey(uiEvent))
                {
                    s_singleUIEvents[uiName][uiEvent] -= callback;
                }
                else
                {
                    Debug.Log("Error: RemoveSingleUIEvent don't exits: " + uiEvent);
                }
            }
            else
            {
                Debug.Log("Error: RemoveSingleUIEvent don't exits: " + uiEvent);
            }
        }

        /// <summary>
        /// 派发UI窗口状态事件
        /// </summary>
        public static void Dispatch(UIWindowBase ui,UIEvent uiEvent,params object[] objs)
        {
            if(ui == null)
            {
                Debug.Log("Error: Dispatch UI is Null !!!");
                return;
            }

            if (s_allUIEvents.ContainsKey(uiEvent))
            {
                try
                {
                    if (s_allUIEvents[uiEvent] != null)
                    {
                        s_allUIEvents[uiEvent].Invoke(ui);
                    }
                }
                catch (Exception ex)
                {
                    Debug.Log("UISystemEvent Dispatch AllUIEvent error :"+ ex.ToString());
                }
            }

            if (s_singleUIEvents.ContainsKey(ui.name))
            {
                if (s_singleUIEvents[ui.name].ContainsKey(uiEvent))
                {
                    try
                    {
                        if (s_singleUIEvents[ui.name][uiEvent] != null)
                        {
                            s_singleUIEvents[ui.name][uiEvent].Invoke(ui);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("UISystemEvent Dispatch SingeUIEvent error :" + ex.ToString());
                    }

                }

            }
        }

    }
}

